<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/30
 * Time: 15:26
 */

//前台首页
namespace Controller;
use Model\IndexModel;
use Shop\Controller;

if(!defined('SHOP')){
    header("Location:/index.php");
}

class IndexController extends TooController{
    public function index(){
        $model=new IndexModel();
        //内容数据--查询所有的栏目和栏目对应的商品
        $lm=$model->pdo_sel("select * from sh_category order by sort desc",2);
        //得到分类项目的6个商品
        foreach($lm as &$v){
            $v['goods']=$model->pdo_sel("select * from sh_goods where c_id = '{$v['id']}' limit 5",2);
        }

        $this->view->assign('lm',$lm);
        $this->view->display();
    }


    //商品列表
    public function goods_list(){
        //栏目
        $lm=$this->model->pdo_sel("select * from sh_category",2);
        $this->view->assign('lm',$lm);

        //拼接条件
        $where='where';
        if($_GET['type']==1){
            $where.=" c_id = '{$_GET['key']}' and";
        }elseif($_GET['type']=='2'){
            $where.=" f_id = '{$_GET['key']}' and";
        }elseif($_GET['type']==3){
            $where.=" name like '%{$_GET['key']}%' and";
        }
        if($where!='where'){
            $where=trim($where,'and');
        }else{
            $where='';
        }

        //排序
        if($_GET['order']){
            $order="order by {$_GET['order']} {$_GET['otype']}";
        }else{
            $order='';
        }

        //分页
        $page=$_GET['page']?$_GET['page']:1;
        $limit=2;
        $oofset=($page-1)*$limit;

        $count=$this->model->pdo_sel("select count(*)as co from sh_goods $where");
        $count=ceil($count['co']/$limit);
        //得到下一页和上一页
        $syy=$page-1>1?$page-1:1;
        $xyy=$page+1<$count?$page+1:$count;
        $this->view->assign('syy',$syy);
        $this->view->assign('xyy',$xyy);

        //查询数据
        $sql="select * from sh_goods $where $order limit $oofset,$limit";
        $goods=$this->model->pdo_sel($sql,2);
        $this->view->assign('goods',$goods);


        $this->view->display();
    }

    //商品详情
    public function goods_msg(){
        $id=$_GET['id'];//商品id
        $sql="select g.*,f.name as fname from sh_goods as g join sh_fl as f on g.f_id = f.id where g.id = $id";
        $goods=$this->model->pdo_sel($sql);//商品
        $goods['img']=explode('|',$goods['img']);//图片

        //规格
        $goods_g=$this->model->pdo_sel("select * from sh_goods_g where g_id = $id",2);

        $this->view->assign('goods',$goods);
        $this->view->assign('id',$id);
        $this->view->assign('goods_g',$goods_g);

        if($_SESSION['Home']['Member']['id']){
            //是否加入收藏
           $is= $this->model->pdo_sel("select count(*) as co from sh_like where m_id='{$_SESSION['Home']['Member']['id']}' and g_id = '{$goods['id']}'");
            if($is['co']>0){
                $type=2;
            }else{
                $type=1;
            }
        }else{
            $type=1;
        }
        $this->view->assign('type',$type);
//        $this->view->display();

        $arr2=$this->model->pdo_sel("select p.*,m.email from sh_pl as p join sh_member as m on m.id=p.m_id and g_id=$id",2);

        $this->view->assign('arr2',$arr2);
        $count=$this->model->pdo_sel("select count(*) as count from sh_pl where g_id=$id");
        $this->view->assign('count',$count);
//        var_dump($count);exit;
        $this->view->display();
    }

    //加入购物车方法
    public function flow(){
        //接收参数
        $arr=array();
        $arr['gg_id']=$_GET['gg_id'];
        $arr['g_id']=$_GET['g_id'];
        $arr['num']=$_GET['num'];
        $arr['time']=time();
        $arr['m_id']=$_GET['u_id'];

        //查询判断是否存在
        $is=$this->model->pdo_sel("select * from sh_flow where m_id = '{$arr['m_id']}' and gg_id='{$arr['gg_id']}' and g_id='{$arr['g_id']}'");
        if($is['id']){
            //修改数据--- 数量相加
            $num=$is['num']+$arr['num'];
            if($this->model->pdo_upd("sh_flow",array('num'=>$num)," where id = '{$is['id']}'")){
                $this->retuAjax(array('type'=>2,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>1,'msg'=>'操作失败'));
            }

        }else{
            //插入数据库
            if($this->model->pdo_insert('sh_flow',$arr)){
                $this->retuAjax(array('type'=>2,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>1,'msg'=>'操作失败'));
            }
        }
    }

    //我的收藏
    public function like(){
        $type=$_GET['type'];
        if($type==1){
            //判断是否加入过收藏
            $is=$this->model->pdo_sel("select count(*) as co from sh_like where g_id = '{$_GET['g_id']}' and m_id = '{$_GET['m_id']}'");
            if($is['co']<1){
                //加入收藏
                $arr=array();
                $arr['m_id']=$_GET['m_id'];
                $arr['g_id']=$_GET['g_id'];
                $arr['time']=time();
                $this->model->pdo_insert('sh_like',$arr);

                //修改商品喜欢值
                $this->model->pdo_exec("update sh_goods set `like` = `like`+1 where id='{$arr['g_id']}'");
            }
        }else{
            //取消收藏
            $this->model->pdo_exec("delete from sh_like where g_id = '{$_GET['g_id']}' and m_id = '{$_GET['m_id']}' limit 1");

            //修改商品喜欢值
            $this->model->pdo_exec("update sh_goods set `like`=`like`-1 where id='{$_GET['g_id']}'");
        }

        $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
    }

    public function pl(){
        if(!empty($_FILES)) {
            $i = 0;
            foreach ($_FILES as $v) {//三维数组转换成2维数组
                if (is_string($v['name'])) { //单文件上传
                    $info[$i] = $v;
                    $i++;
                } else { // 多文件上传
                    foreach ($v['name'] as $key => $val) {//2维数组转换成1维数组
                        //取出一维数组的值，然后形成另一个数组
                        //新的数组的结构为：info=>i=>('name','size'.....)
                        $info[$i]['name'] = $v['name'][$key];
                        $info[$i]['size'] = $v['size'][$key];
                        $info[$i]['type'] = $v['type'][$key];
                        $info[$i]['tmp_name'] = $v['tmp_name'][$key];
                        $info[$i]['error'] = $v['error'][$key];
                        $i++;
                    }
                }
            }
        }
//        var_dump($_SESSION);exit;
        $str='';
        foreach ($info as $v) {
            $str.='|'. $this->imgFile($v,'/Public/Home/img/');
        }
        $arr['txt']=$_POST['content'];
        $arr['img']=$str;
        $arr['g_id']=$_GET['id'];
        $arr['m_id']=$_SESSION['Home']['Member']['id'];
        $arr['time']=time();
        if($this->model->pdo_insert('sh_pl',$arr)){
            echo "<script>alert('提交成功');</script>";
            header("Location:/index.php?C=Index&F=goods_msg&id=$arr[g_id]");
        }

    }

//        smarty使用
    public function sm(){
        $sm='smarty集成';
        $obj=$this->sm;
        var_dump($obj);exit;
        $this->assign('sm',$sm);
        $this->display('sm.html');
    }
}